package com.ruoyi.web.controller.information;

import cn.hutool.core.util.StrUtil;
import com.lx.information.domain.bo.ImportExcelData;
import com.lx.information.domain.InformationJointCase;
import com.lx.information.domain.query.InformationJointCaseQuery;
import com.lx.information.domain.vo.InformationCaseVO;
import com.lx.information.domain.vo.InformationJointCaseVO;
import com.lx.information.domain.vo.InformationVO;
import com.lx.information.service.IInformationJointCaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.InformationService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Api("信息管理接口")
@RestController
@RequestMapping("/information/case")
public class InformationController extends BaseController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private InformationService informationService;
    @Autowired
    private IInformationJointCaseService informationJointCaseService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 上传请求
     *
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public AjaxResult importExcel(MultipartFile file) {
        AjaxResult ajaxResult = informationService.importExcel(file);
        return ajaxResult;
    }

    /**
     * 下载模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ImportExcelData> util = new ExcelUtil<>(ImportExcelData.class);
        util.importTemplateExcel(response, "信息数据");
    }
    /**
     * 查询共同的案件总列表
     */
    @PreAuthorize("@ss.hasPermi('information:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(InformationJointCaseQuery informationJointCaseQuery)
    {
        List<SysUser> users = userService.selectUserList(new SysUser());
        List<Long> userList = users.stream().map(sysUser1 -> sysUser1.getUserId()).collect(Collectors.toList());
        startPage();
        List<InformationJointCase> list = informationJointCaseService.selectInformationJointCaseListByAuth(informationJointCaseQuery, userList);
        List<InformationJointCaseVO> caseVOList = new ArrayList<>();
        for (InformationJointCase jointCase : list) {
            InformationJointCaseVO informationJointCaseVO = new InformationJointCaseVO();
            BeanUtils.copyProperties(jointCase, informationJointCaseVO);
            SysUser sysUser = sysUserService.selectUserById(jointCase.getUserId());
            SysDept sysDept = sysDeptService.recursiveParentUpGetBranchOffice(sysUser.getDeptId(), 0);
            informationJointCaseVO.setBranchOffice(sysDept.getDeptName());
            caseVOList.add(informationJointCaseVO);
        }
        return getDataTable(caseVOList);
    }

    @PreAuthorize("@ss.hasPermi('information:case:info')")
    @GetMapping("/info")
    public AjaxResult info(@NotNull(message = "id不可以为null") Long id) {
        InformationVO informationVO =  informationService.info(id);
        return AjaxResult.success(informationVO);
    }

    @PostMapping("/saveRemarkApi")
    public AjaxResult saveRemarkApi(@RequestBody InformationJointCaseVO informationJointCaseVO) {
        InformationJointCase informationJointCase = informationJointCaseService.selectInformationJointCaseById(informationJointCaseVO.getId());
        if (StrUtil.isNotBlank(informationJointCaseVO.getRemarkOne())) {
            informationJointCase.setRemarkOne(informationJointCaseVO.getRemarkOne());
        }
        if (StrUtil.isNotBlank(informationJointCaseVO.getRemarkTwo())) {
            informationJointCase.setRemarkTwo(informationJointCaseVO.getRemarkTwo());
        }
        if (StrUtil.isNotBlank(informationJointCaseVO.getRemarkThree())) {
            informationJointCase.setRemarkThree(informationJointCaseVO.getRemarkThree());
        }
        informationJointCaseService.updateInformationJointCase(informationJointCase);
        return AjaxResult.success();
    }
}
